'use strict'

let models = require('../models');
let errors = require('../../config/errors');

let Counter = models.Counter;

let CounterDao = models.CounterDao;

function nextIndex(name) {
    if ( !(CounterDao.findOne({
        name: name
    })) ) {
        throw errors.CounterNameNotFound
    }

    let counter = CounterDao.updateOne({
        name: name
    }, {
        $inc: {
            count: 1
        }
    }, {
        new: true
    });

    return counter.count;
}

module.exports = {
    nextIndex
};
